/*
 * 工具函数
 * @Author: ahwgs
 * @Date: 2020-05-25 23:38:31
 * @Last Modified by: yangws
 * @Last Modified time: 2021-02-04 19:53:46
 */
import pathRegexp from 'path-to-regexp';
import { Route } from '@/models/connect';
/**
 * 扁平化路由表
 * @param routes
 */
export const formatterMenu = (routes: any[]) => {
  if (!routes || routes.length < 1) return [];
  const newMenus: any[] = [];
  const flattenMenuData = (data: any[]) => {
    const menus = data.filter((item) => item.name && item.path);
    menus.forEach((item) => {
      const result = {
        ...item,
      };
      if (item.routes) {
        flattenMenuData(item.routes);
      }
      newMenus.push(result);
    });
  };

  flattenMenuData(routes);
  return newMenus;
};

/**
 * 返回所有是菜单的路由
 * @param routes
 */
export const getMenuRoute = (routes: any[]) => {
  if (!routes || routes.length < 1) return [];
  return formatterMenu(routes)
    .filter((r) => r.isMenu)
    .map((r) => r.path);
};

/**
 * props.route.routes
 * @param router [{}]
 * @param pathname string
 */
export const getAuthorityFromRouter = <T extends Route>(
  router: T[] = [],
  pathname: string,
): T | undefined => {
  const authority = router.find(
    ({ routes, path = '/', target = '_self' }) =>
      (path && target !== '_blank' && pathRegexp(path).exec(pathname)) ||
      (routes && getAuthorityFromRouter(routes, pathname)),
  );
  if (authority) return authority;
  return undefined;
};

export const getRouteAuthority = (path: string, routeData: any[]) => {
  let authorities: string[] | string | undefined;
  routeData.forEach((route) => {
    // match prefix
    if (pathRegexp(`${route.path}/(.*)`).test(`${path}/`)) {
      if (route.authority) {
        authorities = route.authority;
      }
      // exact match
      if (route.path === path) {
        authorities = route.authority || authorities;
      }
      // get children authority recursively
      if (route.routes) {
        authorities = getRouteAuthority(path, route.routes) || authorities;
      }
    }
  });
  return authorities;
};
/**
 * 关键信息隐藏
 * @param str 字符串
 * @param frontLen 字符串前面保留位数
 * @param endLen 字符串后面保留位数
 * @returns {string}
 */
export const hideCode = (str: string, frontLen: number, endLen: number,centerLen?:number)=> {
  if(!str){
    return '';
  }
  const len = centerLen || (str.length - frontLen - endLen);
  let xing = '';
  // eslint-disable-next-line no-plusplus
  for (let i = 0; i < len; i++) {
      xing += '*';
  }
  return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
};
export const toChinesNum = (num) => {
  let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //changeNum[0] = "零"
  let unit = ["", "十", "百", "千", "万"];
  num = parseInt(num);
  let getWan = (temp) => {
      let strArr = temp.toString().split("").reverse();
      let newNum = "";
      for (var i = 0; i < strArr.length; i++) {
          newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
      }
      return newNum;
  }
  let overWan = Math.floor(num / 10000);
  let noWan = num % 10000;
  if (noWan.toString().length < 4) noWan = "0" + noWan;
  return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
  
}

